package coms.ejb.sessionbean;

import java.util.*;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import coms.dataaccess.ConferenceDAO;
import coms.dataaccess.DAOFactory;
//import coms.dataaccess.UserDao;
import coms.datatransfer.ConferenceDTO;
import coms.datatransfer.ConferenceItemDTO;
import coms.datatransfer.TransactionDTO;
import coms.exception.RegistrationDateExpiredException;


@Stateless(name="ConferenceBean", mappedName = "ConferenceBean")
@Remote(ConferenceBeanRemote.class)
public class ConferenceBean implements ConferenceBeanRemote {
	
	private ConferenceDAO conferenceDAO = null;
	//private UserDao userDAO = null;
	
	public void ejbCreate(){
		conferenceDAO = DAOFactory.getInstance().getConferenceDAO();
		//userDAO = DAOFactory.getInstance().getUserDAO();
		//conferenceDAO.initialize();
	}
	
	
	public Integer createConference(ConferenceDTO conference){	
		
		for (ConferenceItemDTO c : conference.getListOfConferenceItems()){
			if (c.getConferenceItemType() == 1){
				c.setMaxReviewers(5);
			}else if (c.getConferenceItemType() == 2){
				c.setMaxReviewers(2);
			}else if (c.getConferenceItemType() == 3){
				c.setMaxReviewers(3);
			}
		}		
		return conferenceDAO.insertConference(conference);
	}

	public ConferenceDTO findConference(int id){
		return conferenceDAO.findConferenceByID(id);		
	}
	
	@Override
	public String testMethod() {
		TransactionDTO t = new TransactionDTO();
		t.setCardNumber("12344");
		t.setConferenceItemID(18);
		t.setPaidAmount(12);
		t.setPaymentDate(new Date());
		t.setPaymentStatus("success");
		t.setUserID(2);
		//TransactionDAO transactionDAO = new TransactionDAOImpl();
		//boolean status = transactionDAO.makePayment(t);
		conferenceDAO.changeStatus(t.getConferenceItemID(), t.getUserID());

		return "Message from Conference";		
	}

	@Override
	public void createConferenceItem(ConferenceItemDTO conferenceItem, int conferenceID) {
		conferenceDAO.insertConferenceItem(conferenceItem,  conferenceID);		
	}


	@Override
	public List<ConferenceDTO> findAllConferences() {
		System.out.println("calling bean");
		List<ConferenceDTO> conferences = conferenceDAO.findAllConferences(); 
		return conferences;
	}


	@Override
	public void registerParticipant(int conferenceItemID, int userID) throws RegistrationDateExpiredException {
		conferenceDAO.registerParticipant(conferenceItemID, userID);
	}


	@Override
	public List<ConferenceItemDTO> findConfirmedConferenceItems(int userID) {
		/*UserDTO user = userDAO.findUserByID(userID);
		Set<ConferenceItemDTO> conferenceItems = user.getListOfConferenceItems();
		for(ConferenceItemDTO conferenceItem: conferenceItems){
			if(conferenceItem.getStatus().equals())
		}*/
		
		return null;
	}

	@Override
	public List<ConferenceItemDTO> findRegisteredConferenceItems(int uID) {
		
		System.out.println("List method:"+uID);
		 List<ConferenceItemDTO> confItems = new  ArrayList<ConferenceItemDTO>();
		 List<Object[]> confUseritems = conferenceDAO.findRegisteredConferenceItems(uID);
		 for (Object[] a : confUseritems)
		 {
			 confItems.add((ConferenceItemDTO) a[0]);
		 }
		 System.out.println("List confITEMS:"+confItems.size());
		return confItems;
	}
	
	
}
